package cn.yh.register.hosp.api;

import cn.yh.register.common.exception.RegisterException;
import cn.yh.register.common.helper.HttpRequestHelper;
import cn.yh.register.common.result.Result;
import cn.yh.register.common.result.ResultCodeEnum;
import cn.yh.register.hosp.service.DepartmentService;
import cn.yh.register.hosp.service.HospitalService;
import cn.yh.register.hosp.service.HospitalSetService;
import cn.yh.register.hosp.service.ScheduleService;
import cn.yh.register.model.hosp.Department;
import cn.yh.register.model.hosp.Hospital;
import cn.yh.register.model.hosp.Schedule;
import cn.yh.register.vo.hosp.DepartmentQueryVo;
import cn.yh.register.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "数据上传API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    HospitalService hospitalService;
    @Autowired
    HospitalSetService hospitalSetService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    ScheduleService scheduleService;

    @ApiOperation("上传医院")
    @PostMapping("/saveHospital")
    public Result<?> savaHospital(HttpServletRequest request) {
        //处理参数 & 校验签名
        Map<String, Object> objectMap = this.handleParam(request);
        //调service
        hospitalService.savaHospital(objectMap);
        return Result.ok();
    }

    @ApiOperation("查询医院")
    @PostMapping("/hospital/show")
    public Result<?> hospitalShow(HttpServletRequest request) {
        //处理参数 & 校验签名
        Map<String, Object> objectMap = this.handleParam(request);
        String hoscode = (String) objectMap.get("hoscode");
        //调service
        Hospital hospital = hospitalService.findHospital(hoscode);
        return Result.ok(hospital);
    }

    @ApiOperation(value = "上传科室")
    @PostMapping("/saveDepartment")
    public Result<?> saveDepartment(HttpServletRequest request) {
        //处理参数 & 校验签名
        Map<String, Object> objectMap = this.handleParam(request);
        //调service
        departmentService.saveDepartment(objectMap);
        return Result.ok();

    }

    @ApiOperation("分页查询科室列表")
    @PostMapping("/department/list")
    public Result<Page<Department>> departmentList(HttpServletRequest request) {
        //处理参数 & 校验签名
        Map<String, Object> objectMap = this.handleParam(request);
        //pageNo
        Integer pageNo = objectMap.get("page") == null ? 1 : Integer.parseInt(objectMap.get("page").toString());
        //pageSize
        Integer pageSize = objectMap.get("limit") == null ? 10 : Integer.parseInt(objectMap.get("limit").toString());
        //查询条件
        String hoscode = (String) objectMap.get("hoscode");
        DepartmentQueryVo queryVo = new DepartmentQueryVo();
        queryVo.setHoscode(hoscode);
        //调service
        Page<Department> departments = departmentService.finDeptList(pageNo, pageSize, queryVo);
        return Result.ok(departments);
    }

    @ApiOperation("删除科室")
    @PostMapping("/department/remove")
    public Result<?> departmentRemove(HttpServletRequest request){
        //处理参数 & 校验签名
        Map<String, Object> objectMap = this.handleParam(request);
        //医院编号
        String hoscode = (String) objectMap.get("hoscode");
        //科室编号
        String depcode = (String) objectMap.get("depcode");
        //调service
        departmentService.removeDepartment(hoscode,depcode);
        return Result.ok();
    }

    @ApiOperation("上传排班")
    @PostMapping("/saveSchedule")
    public Result<?> saveSchedule(HttpServletRequest request){
        //处理参数 & 校验签名
        Map<String, Object> objectMap = this.handleParam(request);
        //调service
        scheduleService.saveSchedule(objectMap);
        return Result.ok();
    }

    @ApiOperation("分页查询排班列表")
    @PostMapping("/schedule/list")
    public Result<Page<Schedule>> scheduleList(HttpServletRequest request){
        //处理参数 & 校验签名
        Map<String, Object> objectMap = this.handleParam(request);
        //pageNo
        Integer pageNo = objectMap.get("page") == null ? 1 : Integer.parseInt(objectMap.get("page").toString());
        //pageSize
        Integer pageSize = objectMap.get("limit") == null ? 10 : Integer.parseInt(objectMap.get("limit").toString());
        //查询条件
        ScheduleQueryVo queryVo = new ScheduleQueryVo();
        String hoscode = (String) objectMap.get("hoscode");
        queryVo.setHoscode(hoscode);
        //调service
        Page<Schedule> scheduleList = scheduleService.getScheduleList(pageNo, pageSize, queryVo);
        return Result.ok(scheduleList);
    }

    @ApiOperation("删除排班")
    @PostMapping("/schedule/remove")
    public Result<?> scheduleRemove(HttpServletRequest request){
        //处理参数 & 校验签名
        Map<String, Object> objectMap = this.handleParam(request);
        //医院编号
        String hoscode = (String) objectMap.get("hoscode");
        //排班编号
        String hosScheduleId = (String) objectMap.get("hosScheduleId");
        //调service
        scheduleService.removeSchedule(hoscode,hosScheduleId);
        return Result.ok();
    }

    /**
     * 处理参数 & 校验签名
     * @param request 请求对象
     * @return Map<String, Object>  objectMap
     */
    private Map<String, Object> handleParam(HttpServletRequest request) {
        //获取参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        //转换map
        Map<String, Object> objectMap = HttpRequestHelper.switchMap(parameterMap);
        //接收到的参数中logonData中的“+”被替换成了“ ”，所以我们要替换回去，否则会导致签名校验失败
        String logoData = (String) objectMap.get("logoData");
        if (!StringUtils.isEmpty(logoData)) {
            logoData = logoData.replaceAll(" ", "+");
            objectMap.put("logoData", logoData);
        }
        //医院编号
        String hoscode = (String) objectMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new RegisterException(ResultCodeEnum.PARAM_ERROR);
        }
        //获取签名key
        String stringKey = hospitalSetService.getStringKey(hoscode);
        //校验签名
        if (!HttpRequestHelper.isSignEquals(objectMap, stringKey)) {
            throw new RegisterException(ResultCodeEnum.SIGN_ERROR);
        }
        return objectMap;
    }

}
